package com.android.mios.adapter;

import java.net.URLEncoder;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import com.android.mios.R;
import com.android.mios.intenet.PollLoop;
import com.android.mios.intenet.WebData;
import com.android.mios.model.Scene;
import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class SceneGridViewAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<Scene> mList;
	private int resource;
	// private String strClose;
	private String strRun;
	private Context mContext;
	private final ConcurrentHashMap<Integer, Boolean> mCache = new ConcurrentHashMap<Integer, Boolean>();

	public static final String TEXT_TITLE = "texttitle";
	public static final String TEXT_DESCRIPTION = "textDescription";

	public SceneGridViewAdapter(Context context, List<Scene> data, int resource) {

		// astrClose = context.getString(R.string.close);
		strRun = context.getString(R.string.run);
		mInflater = LayoutInflater.from(context);
		mList = data;
		this.resource = resource;
		mContext = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null)
			convertView = mInflater.inflate(resource, null);

		final Scene mScene = mList.get(position);

		final int id = mScene.id;

		final ProgressBar progress = (ProgressBar) convertView
				.findViewById(R.id.progress_loading);
		progress.setTag(id);

		if (mCache.get(id) == null)
			progress.setVisibility(View.GONE);
		else
			progress.setVisibility(View.VISIBLE);

		TextView txt_name = (TextView) convertView
				.findViewById(R.id.txt_scene_name);
		txt_name.setText(mScene.toString());

		Button btn = (Button) convertView.findViewById(R.id.btn_scene_status);

		btn.setText(strRun);

		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (progress.getVisibility() != View.VISIBLE)
					new LoadAsyncTask(progress, id, parent, mScene.serialNumber)
							.execute();
			}
		});

		return convertView;
	}

	class LoadAsyncTask extends AsyncTask<Void, Void, String> {

		ProgressBar progress;
		int id;
		ViewGroup parent;
		int serialNumber;

		private LoadAsyncTask(ProgressBar progress, int id, ViewGroup parent,
				int serialNumber) {
			this.progress = progress;
			this.id = id;
			this.parent = parent;
			this.serialNumber = serialNumber;
		}

		@Override
		protected void onPreExecute() {

			progress.setVisibility(View.VISIBLE);
			mCache.put(id, true);

		}

		protected String doInBackground(Void... params) {

			return WebData
					.getInstance()
					.sendServiceActionWithResponse(
							PollLoop.getInstance().getEngineForSerialNumber(
									serialNumber),
							"action",
							URLEncoder
									.encode("urn:micasaverde-com:serviceId:HomeAutomationGateway1"),
							"RunScene", "SceneNum=" + id);

		}

		@Override
		protected void onPostExecute(String result) {

			ProgressBar progress = (ProgressBar) parent.findViewWithTag(id);
			if (progress != null)
				progress.setVisibility(View.GONE);
			mCache.remove(id);

			if (result.equals("")) {
				Toast.makeText(mContext, R.string.operation_fails,
						Toast.LENGTH_SHORT).show();

			} else if (result.startsWith("ERROR")) {
				Toast.makeText(mContext, result.substring(6),
						Toast.LENGTH_SHORT).show();
			} else
				Toast.makeText(mContext, R.string.scene_run, Toast.LENGTH_LONG)
						.show();

		}
	};

}
